package com.bizihang.validate.code.core.support.code;

import com.bizihang.validate.code.core.ValidateCode;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

import java.time.LocalDateTime;

/**
 * 验证码基本类
 */
public class TextValidateCode implements ValidateCode {
    private String code;
    private int length;

    /**
     * 过期时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private transient LocalDateTime expireTime;

    /**
     * @param code         验证码
     * @param expireDuring 过期时间间隔
     */
    public TextValidateCode(String code, int expireDuring) {
        this.code = code;
        this.expireTime = LocalDateTime.now().plusSeconds(expireDuring);
    }

    /**
     * @param code       验证码
     * @param expireTime 过期时间
     */
    public TextValidateCode(String code, LocalDateTime expireTime) {
        this.code = code;
        this.expireTime = expireTime;
    }

    @Override
    @JsonIgnore
    public boolean isExpired() {
        return LocalDateTime.now().isAfter(expireTime);
    }

    @Override
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }
}
